<?php
namespace app\cli;

use think\console\Command;
use think\console\Input;
use think\console\Output;
use app\common\model\TableModel;
use app\handle\Game;
use app\handle\Table;
use think\template\driver\File;

// 自动开奖脚本
class AutoRecyling extends Command
{
    protected function configure()
    {
        $this->setName('AutoRecyling')
            ->setDescription('AutoRecyling script');
    }

    protected function execute(Input $input, Output $output)
    {
        $this->recyling($input, $output);
    }

    // 自动回收牌桌
    protected function recyling(Input $input, Output $output)
    {
        /**
         * 每日12点执行
         * 获取今日之前的牌桌
         * 获取所有牌桌缓存信息
         * 如果牌桌正在游戏中或者里面有人则不清除，否则全部清空
         */
        $table = new TableModel;

        $tableList = $table->whereTime('create_time', '<', date('Y-m-d'))->all();
        foreach ($tableList as $k => $v) {
            $hgame  = new Game($v['id'], 0);
            $htable = new Table($v['id'], 0);

            $pos = $htable->getTablePos();
            if (count($pos) > 0) {
                echo "存在人的牌桌:{$v['id']}\n";
            } else {
                $msg = date('Y-m-d H:i:s')." =====> 自动回收牌桌:{$v['id']}\n";
                $table->destroy($v['id']);
                $this->writeFile($msg);
                echo $msg;
            }
        }
    }

    // 写入文件
    protected function writeFile($msg)
    {
        $file = new File();
        $path = 'public/buildIds/recyling.logs';
        $file->mywrite($path, $msg);
    }
}

